<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
        }

        .table .item {
            float: left;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名<input v-model="username"/></div>
            <div>密码<input v-model="userpwd"/></div>
            <button @click="add">新增</button>
        </div>
        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userpwd}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>
        </div>

    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username:"",
            userpwd:"",//声明
            list: [],//声明数据的格式  是一个集合
        },
        methods:{
          async  add(){
                //确保页面取值没有问题
                //alert(this.username+"和"+this.userpwd);
                let  {data}  = await axios({
                    method:"post",
                    url:"http://localhost:2346/userinfos",
                    data:{//这里需要注意  get才是parmas 其他都是data
                        username:this.username,
                        userpwd:this.userpwd
                    }
                })
                //同步页面数据
                this.list.push(data);




            },

          async  del(id){
               // alert(id);
                //1:绑定事件  验证事件和参数调用成功
                await  axios({
                    method:"delete",
                    url:"http://localhost:2346/userinfos/"+id
                });//删除数据库的数据

                this.list = this.list.filter(item=>item.id!=id);
                //同步页面的数据


            }
        },
        async created() {//里面可以进行数据请求
            let {data:list} = await axios({ // {data:list}解构
                method: "get",//表示是get请求
                url: "http://localhost:2346/userinfos",
                params: {},//参数
            })
            //1:看network 判断是否有请求
            //2:输出请求的结果
            // console.log(res);
            //3:赋值给this  在看vue中数据
            this.list = list;
            //4:把数据渲染到页面
        }

    })


</script>

</html>